//
//  TSResultCollectionViewCell.swift
//  TSProfitProject
//
//  Created by TMS on 2019/11/15.
//  Copyright © 2019 TMS. All rights reserved.
//

import UIKit
import HandyJSON

struct TSResultModel : HandyJSON {
    var title : Int = 0
    var isSelected : Bool = false
}

protocol TSResultCollectionViewCellDelegate : NSObjectProtocol {
    func didClickItem(_ model:TSResultModel)
}

class TSResultCollectionViewCell: UICollectionViewCell {
    
    weak var delegate : TSResultCollectionViewCellDelegate?
    private var itemBg : UIImageView!
    private var label : UILabel!
    private var model : TSResultModel!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        configViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupCell(model:TSResultModel) {
        self.model = model
        
        label.text = String.init(format: "%d", model.title)
        if model.isSelected {
            itemBg.image = UIImage(named: "main_invest_selected")
            label.textColor = WHITE_COLOR
        } else {
            itemBg.image = UIImage(named: "main_invest_normal")
            label.textColor = BASE_PURPLE_COLOR
        }
    }
    
    @objc func btnAction() {
        if self.delegate != nil {
            self.delegate?.didClickItem(model)
        }
    }
    
    private func configViews() {
        
        itemBg = UIImageView.init()
        itemBg.backgroundColor = WHITE_COLOR
        contentView.addSubview(itemBg)
        itemBg.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        label = UILabel.init()
        label.textColor = BASE_PURPLE_COLOR
        label.font = UIFont.systemFont(ofSize: 12, weight: .medium)
        label.textAlignment = .center
        contentView.addSubview(label)
        label.snp.makeConstraints { (make) in
            make.centerY.left.right.equalToSuperview()
        }
        
    }
}
